<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class CommentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        
        $user = \Auth::user();

        $reply_msg = null;
        if($this->reply_id){
            $reply_msg = [
                'id'=>$this->quote->id,
                'user_id'=>$this->quote->user->id,
                'user_name'=>$this->quote->user->name?$this->quote->user->name:'游客'.$this->quote->user->id,
                'user_avatar'=>$this->quote->user->avatar,
                'body'=>$this->quote->body,
            ];
        }
        
        $this->user->name = $this->user->name?$this->user->name:'游客'.$this->user->id;
        $this->user->avatar = $this->user->avatar?$this->user->avatar:'';
        return [
            'id'   => (string)$this->id,
            'user' => $this->user,
            'body' => $this->body,
            'rank'  => $this->rank,
            'reply_id'  => $this->reply_id,
            'liked'  => $this->liked, //点赞数
            'quote'=> $reply_msg,
            'userliked'=>$user && $user->hasLiked($this->resource), // todo待优化(访问速度慢)
            'commented'  => $this->commented, //评论数
            'wxto'  => '/pages/article/info?id='.$this->id,
            'time' => $this->created_at->from(),
        ];
    }
}
